﻿/*
	Sentinel Action Script Packages
	Picture Changer Class v1.1
	Copyright (C) 2008  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License any
	later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Sentinel Action Script Packages Copyright (C) 2008 Önder Ezdi
    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.
*/

/**
 * PictureChanger
 * 
 * Creates a display object which displays objects added to it
 * (with addImage) with selected effect.
 */

package makeeasier.utils {
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.filters.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	public class PictureChanger extends Sprite {
		
		private var imageContainer:Sprite;
		private var imageList:Array;
		private var BitmapList:Array;
		private var timer:Timer;
		private var timerInterval:Number;
		private var currentEffect:Number;
		private var currentImage:Number;
		private var prevImage:Number;
		private var antiGC:Dictionary;
		private var effectCounterValue:Number;
		
		public function PictureChanger(){
			imageList = new Array();
			BitmapList = new Array();
			antiGC = new Dictionary(false);
			timerInterval = 3000; // default timer interval
			currentEffect = 0;
			timer = new Timer(timerInterval);
			timer.addEventListener(TimerEvent.TIMER, ChangeImage);
		}
		
		public function AddImage(image:*){
			imageList.push(image);
			CreateScene();
			SetImagesToCenter();
		}
		
		private function CreateScene():void{
			if(imageContainer!=null){
				removeChild(imageContainer);
				BitmapList = new Array();
			}
			imageContainer = new MovieClip();
			var len:Number = imageList.length;
			for( var i:Number=0; i<len; i++ ){
				var mc:MovieClip = new MovieClip();
				mc.addChild(imageList[i]);
				BitmapList[i] = mc;
				BitmapList[i].alpha = 0;
				imageContainer.addChild(BitmapList[i]);
			}
			imageContainer.x = (imageContainer.width / 2);
			imageContainer.width;
			addChild(imageContainer);
			currentImage = 0;
			prevImage = BitmapList.length;
		}
		
		private function SetImagesToCenter():void{
			var maxWidth = 0;
			var maxHeight = 0;
			for( var i:Number=0; i<BitmapList.length; i++ ){
				if( BitmapList[i].width > maxWidth ){ maxWidth = BitmapList[i].width; }
				if( BitmapList[i].height > maxHeight ){ maxHeight = BitmapList[i].height; }
			}
			for( var j:Number=0; j<BitmapList.length; j++ ){
				BitmapList[j].firstX = BitmapList[j].getChildAt(0).x = (maxWidth / 2)-(BitmapList[j].width/2);
				BitmapList[j].firstY = BitmapList[j].getChildAt(0).y = (maxHeight / 2)-(BitmapList[j].height/2);
				
			}
		}
		
		public function set Interval(n:Number):void{
			timerInterval = n;
			timer = new Timer(timerInterval);
			timer.addEventListener(TimerEvent.TIMER, ChangeImage);
		}
		
		public function get Interval():Number{
			return timerInterval;
		}
		
		public function Start():void{
			ChangeImage(null);
			timer.start();
		}
		
		public function Stop():void{
			timer.stop();
		}
		
		public function set Effect(n:Number):void{
			currentEffect = n;
		}
		
		public function get Effect():Number{
			return currentEffect; 
		}
		
		private function ChangeImage(e:Event):void{
			if(BitmapList.length<2){
				BitmapList[currentImage].alpha = 1;
				return;
			}
			//TODO: Effect system
			BitmapList[currentImage].filters = null;
			var tween:Tween = new Tween( BitmapList[currentImage], "alpha", Strong.easeOut, 0, 1, 2, true );
			tween.addEventListener(TweenEvent.MOTION_CHANGE, SetEffect);
			antiGC[tween] = tween;
			setTimeout( RemoveImage, timerInterval , BitmapList[currentImage] );
			if ( currentImage < imageList.length-1 ){
				currentImage++;
			} else {
				currentImage = 0;
			}
		}
		
		private function RemoveImage( ob ):void{
			var tween2:Tween = new Tween( ob, "alpha", Strong.easeOut, 1, 0, 2, true );
			tween2.addEventListener(TweenEvent.MOTION_CHANGE, SetEffect);
			tween2.addEventListener(TweenEvent.MOTION_FINISH, ResetItem);
			antiGC[tween2] = tween2;
		}
		
		private function SetEffect(e:Event):void{
			switch (currentEffect){
				case 1: SetBlurHorizantal(e); break;
				case 2: SetBlurVertical(e); break;
				case 3: SetBlurBoth(e); break;
				case 4: RollingEffect(e); break;
				default: break;
			}
		}
		
		private function SetBlurHorizantal(e:Event):void{
			var bf:BlurFilter = new BlurFilter(30*(1-e.target.obj.alpha),0*(1-e.target.obj.alpha),3);
			e.target.obj.filters = [bf];
		}
		
		private function SetBlurVertical(e:Event):void{
			var bf:BlurFilter = new BlurFilter(0*(1-e.target.obj.alpha),30*(1-e.target.obj.alpha),3);
			e.target.obj.filters = [bf];
		}
		
		private function SetBlurBoth(e:Event):void{
			var bf:BlurFilter = new BlurFilter(30*(1-e.target.obj.alpha),30*(1-e.target.obj.alpha),3);
			e.target.obj.filters = [bf];
		}
		
		private function RollingEffect(e:Event):void{
			e.target.obj.y = (e.target.obj.firstY * (1 - (1 * e.target.obj.alpha)));
			e.target.obj.scaleY = (1-e.target.obj.alpha)+(1*e.target.obj.alpha);
		}
		
		private function FallEffect(e:Event):void{
			e.target.obj.y = (e.target.obj.firstY * (1 - (1 * e.target.obj.alpha)));
			e.target.obj.scaleY = 1/(1*e.target.obj.alpha);
		}
		
		private function ResetItem(e:Event):void{
			e.target.obj.x = 0;
			e.target.obj.y = 0;
			e.target.obj.scaleX = 1;
			e.target.obj.scaleY = 1;
			e.target.obj.alpha = 0;
		}
		
		
		
	}
	
}